import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * TODO
 * className: KeyWordsCountObject
 * package: PACKAGE_NAME
 * author: cosco
 * mail: gt.fan@msn.cn
 * create 2018-11-27 23:01
 **/

public class KeyWordsCountObject implements KeyWordsCount {

    private int count = 0;

    Lock lock = new ReentrantLock();

    ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    Lock readLock = readWriteLock.readLock();
    Lock writeLock = readWriteLock.writeLock();

//    @Override
    public  int addCount5(int count) {
        System.out.println("增加次数："+count);
        this.count += count;
        return this.count;
    }

    public synchronized int addCount4(int count) {
        System.out.println("增加次数："+count);
        this.count += count;
        return this.count;
    }

    @Override
    public int addCount(int count) {
        System.out.println("增加次数："+count);
        lock.tryLock();
        try{
            this.count += count;
        }
        finally {
            lock.unlock();
        }

        return this.count;
    }

    @Deprecated
    public int addCount3(int count) {
        System.out.println("增加次数："+count);
        writeLock.lock();
        try{
            this.count += count;
        }
        finally {
            writeLock.unlock();
        }

        return this.count;
    }

    @Override
    public int getCount() {
        return this.count;
    }
}
